package com.bq.gametrade.ui.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bq.gametrade.R;

/**
 * 功能：
 * <p>
 *
 * @author Feel on 2016/12/8 11:16
 */

public class LoadingProgressDialog extends AlertDialog {

    private CharSequence message;
    private AnimationDrawable spinner;

    public LoadingProgressDialog(Context context) {
        this(context, R.style.Custom_Progress);
    }

    public LoadingProgressDialog(Context context, CharSequence message) {
        this(context);
        this.message = message;
    }

    public LoadingProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    public LoadingProgressDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context,cancelable,cancelListener);
    }

    /**
     * 当窗口焦点改变时调用
     */
    public void onWindowFocusChanged(boolean hasFocus) {
        ImageView imageView = (ImageView) findViewById(R.id.spinnerImageView);
        // 获取ImageView上的动画背景
         spinner = (AnimationDrawable) imageView.getBackground();
        // 开始动画
        spinner.start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_loadingprogress);
        setMessage();
        // 按返回键是否取消
        setCancelable(true);
        // 监听返回键处理
        setCanceledOnTouchOutside(false);

//        // 设置居中
//        getWindow().getAttributes().gravity = Gravity.CENTER;
//        WindowManager.LayoutParams lp = getWindow().getAttributes();
//        // 设置背景层透明度
//        lp.dimAmount = 0.2f;
//        getWindow().setAttributes(lp);
//        // dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
    }

    /**
     * 给Dialog设置提示信息
     *
     * @param
     */
    private void setMessage() {
        TextView txt = (TextView) findViewById(R.id.message);
        if (message != null && message.length() > 0) {
            txt.setText(message);
            txt.invalidate();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(spinner !=null){
            spinner.stop();
        }

    }

    /**
     * 弹出自定义ProgressDialog
     *
     * @param context        上下文
     * @param message        提示
     * @param cancelable     是否按返回键取消
     * @param cancelListener 按下返回键监听
     * @return
     */
    public static LoadingProgressDialog show(Context context, CharSequence message, boolean cancelable, OnCancelListener cancelListener) {
        LoadingProgressDialog dialog = new LoadingProgressDialog(context);
        dialog.setTitle("");
        dialog.setContentView(R.layout.dialog_loadingprogress);

        // 按返回键是否取消
        dialog.setCancelable(cancelable);
        // 监听返回键处理
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnCancelListener(cancelListener);
        // 设置居中
        dialog.getWindow().getAttributes().gravity = Gravity.CENTER;
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        // 设置背景层透明度
        lp.dimAmount = 0.2f;
        dialog.getWindow().setAttributes(lp);
        // dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        dialog.show();
        return dialog;
    }
}
